
<template>
    <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">            
            <el-form-item label="类型" prop="key" >
                <el-input v-model="form.key" placeholder="请输入类型"  />
            </el-form-item>            
            <el-form-item label="名称" prop="name" >
                <el-input v-model="form.name" placeholder="请输入名称"  />
            </el-form-item>            
            <el-form-item label="前缀" prop="prefix" >
                <el-input v-model="form.prefix" placeholder="请输入前缀"  />
            </el-form-item>            
            <el-form-item label="时间格式" prop="time" >
                <el-input v-model="form.time" placeholder="请输入时间格式"  />
            </el-form-item>            
            <el-form-item label="流水号起点" prop="serialBegin" >
                <el-input-number v-model="form.serialBegin" ></el-input-number>
            </el-form-item>            
            <el-form-item label="流水号长度" prop="serialLength" >
                <el-input-number v-model="form.serialLength" ></el-input-number>
            </el-form-item>            
            <el-form-item label="当前流水号" prop="serialCounter" >
                <el-input-number v-model="form.serialCounter" ></el-input-number>
            </el-form-item>            
        </el-form>        
    </div>
</template>
<script>
import {
    getCodeRule,
    createCodeRule,
    addCodeRule,
    updateCodeRule,    
} from "@/api/basic/codeRule";
import { getUserProfile } from "@/api/system/user";
import {genCode} from '@/api/code.js';
import dayjs from 'dayjs'
export default {
    name: "CodeRuleEditor",
    components:{        
    },
    props: [        
    ],
    data() {
        return {            
            // 表单参数
            form: {},
            // 表单校验
            rules: {                
            },
            user:{}
        };
    },
    created() {
        this.getUser();
    },
    methods: {
        // 表单重置
        reset() {
            // 重新加载选项            
            // 重置表单值
            this.form = {                
                id: undefined,                
                key: undefined,                
                name: undefined,                
                prefix: undefined,                
                time: undefined,                
                serialBegin: undefined,                
                serialLength: undefined,                
                serialCounter: undefined,                
            };
            // 重置相关参数            
            this.resetForm("form");
        },        
        // 加载一条已经存在的数据
        load(id, done) {
            this.reset();
            getCodeRule(id).then(response => {
                let data = response.data;                
                this.form = data;
                done()
            });
        },
        // 提前提交
        precreate(){
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (!this.form.id) {
                        createCodeRule(this.form).then(response => {
                            if (response.code === 0) {
                                this.form.id = response.data.id;
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        // 正常提交
        submitForm: function(done) {
            this.$refs["form"].validate(valid => {
                if (valid) {                    
                    if (this.form.id != undefined) {
                        updateCodeRule(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("修改成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addCodeRule(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("新增成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        genCode(){            
        },
        getUser() {
            getUserProfile().then(response => {
                this.user = response.data;
            });
        },        
    }
};
</script>
<style>    
</style>